package com.example.teaching_server.entity;

import jakarta.persistence.*;
import jakarta.validation.constraints.NotBlank;
import jakarta.validation.constraints.PositiveOrZero;
import lombok.*;
import org.hibernate.annotations.CreationTimestamp;
import org.hibernate.annotations.UpdateTimestamp;
import com.fasterxml.jackson.annotation.JsonFormat;
import java.time.LocalDateTime;

/**
 * 课程实体类
 */
@Getter
@Setter
@ToString
@EqualsAndHashCode(of = "id")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Table(name = "courses", indexes = {
        @Index(name = "idx_course_name", columnList = "course_name"),
        @Index(name = "idx_category", columnList = "category")
})
public class CourseEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Column(name = "course_id")
    private Long id;

    @Column(name = "course_name", nullable = false)
    @NotBlank(message = "课程名称不能为空")
    private String courseName;

    @Column(nullable = false)
    @PositiveOrZero(message = "学分必须大于等于0")
    @Builder.Default
    private Integer credit = 0;

    @Column(nullable = false)
    @NotBlank(message = "课程分类不能为空")
    private String category;

    // 保持原有的getter方法名称以兼容现有代码
    public Long getCourseId() {
        return id;
    }

    public void setCourseId(Long courseId) {
        this.id = courseId;
    }

    @Transient
    public String getFullCourseInfo() {
        return courseName + " (" + credit + "学分)";
    }

    @PreUpdate
    private void validateCredit() {
        if (credit != null && credit < 0) {
            throw new IllegalArgumentException("学分不能为负数");
        }
    }
}
